/******************************************************************
*
* Copyright 2010 Darshan M Sonde
*
* This file is part of ddots.
*
* ddots is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ddots is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ddots.  If not, see <http://www.gnu.org/licenses/>.
*
*********************************************************************/

#include "engineinterface.h"
#include "level1.h"

/* This is a second level in which , if computer can complete
   a box, it will complete otherwise it will play like level 0
   that is random.
   */
Level1Engine::Level1Engine()
    :mName("Level 1")
{
};

void Level1Engine::initGame(int rows,int cols)
{
    mValidMoves.clear();
    mRows = rows;
    mColumns = cols;
    for(int i=0;i<rows;++i)
    {
        for(int j=0;j<cols;++j)
        {
            if( j != cols-1 )
            {
                mValidMoves.append(
                        QLine(QPoint(i,j),QPoint(i,j+1)) );
            }

            if( i != rows-1 )
            {
                mValidMoves.append(
                        QLine(QPoint(i,j),QPoint(i+1,j)) );
            }
        }
    }
    qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
}

bool Level1Engine::doMove(int startRow, int startCol, int endRow, int endCol)
{
    if(mValidMoves.isEmpty())
        return false;

    foreach(QLine line,mValidMoves) {
        if( (line.x1()==startRow)
            &&(line.y1()==startCol)
            &&(line.x2()==endRow)
            &&(line.y2()==endCol) ) {
            mValidMoves.removeOne(line);
            return true;
        }
    }
    return false;
}

bool Level1Engine::getMove(int &startRow, int &startCol, int &endRow, int &endCol)
{
    if(mValidMoves.size()==0)
        return false;

    if(canCompleteBox(startRow,startCol,endRow,endCol)) {
        foreach(QLine line,mValidMoves) {
            if( (line.x1()==startRow)
                &&(line.y1()==startCol)
                &&(line.x2()==  endRow)
                &&(line.y2()==endCol) ) {
                mValidMoves.removeOne(line);
                return true;
            }
        }
        return true;//values are copied to respective variables
    }
    //else random like level0

    int val=qrand()%mValidMoves.size();
    QLine line=mValidMoves.takeAt(val);
    startRow = line.x1();
    startCol = line.y1();
    endRow = line.x2();
    endCol = line.y2();
    return true;
}

/* need to check if can complete any box in entire grid*/
bool Level1Engine::canCompleteBox(int &startRow, int &startCol, int &endRow, int &endCol)
{
    for(int i=0;i<mRows-1;++i)
    {
        for(int j=0;j<mColumns-1;++j)
        {
            switch(checkBox(i,j))
            {
                case 0: // top line
                    startRow = i;
                    startCol = j;
                    endRow = i;
                    endCol = j+1;
                    return true;
                case 1:// right line
                    startRow = i;
                    startCol = j+1;
                    endRow = i+1;
                    endCol = j+1;
                    return true;
                case 2://bottom line
                    startRow = i+1;
                    startCol = j;
                    endRow = i+1;
                    endCol = j+1;
                    return true;
                case 3://left line
                    startRow = i;
                    startCol = j;
                    endRow = i+1;
                    endCol = j;
                    return true;
                default://cannot complete a box
                    break;
            }
        }
    }
    return false;
}

int Level1Engine::checkBox(int startRow, int startCol)
{
    Q_ASSERT(startRow<mRows-1);
    Q_ASSERT(startCol<mColumns-1);

    bool topLineEmpty=false;
    bool rightLineEmpty=false;
    bool leftLineEmpty=false;
    bool bottomLineEmpty=false;

    if(mValidMoves.contains(topLine(startRow,startCol)))
        topLineEmpty=true;

    if(mValidMoves.contains(bottomLine(startRow,startCol)))
        bottomLineEmpty=true;

    if(mValidMoves.contains(rightLine(startRow,startCol)))
        rightLineEmpty=true;

    if(mValidMoves.contains(leftLine(startRow,startCol)))
        leftLineEmpty=true;

    //if any three are filled then there is one box completable.

    //top / 0
    if(topLineEmpty &&
       !(rightLineEmpty||bottomLineEmpty||leftLineEmpty))
        return 0;

    if(rightLineEmpty &&
       !(bottomLineEmpty||leftLineEmpty||topLineEmpty))
        return 1;

    if(bottomLineEmpty &&
       !(leftLineEmpty||topLineEmpty||rightLineEmpty))
        return 2;

    if(leftLineEmpty &&
       !(topLineEmpty||rightLineEmpty||bottomLineEmpty))
        return 3;

    return -1;


}

QLine Level1Engine::topLine(int startRow, int startCol)
{
    return QLine(startRow,startCol,startRow,startCol+1);
}

QLine Level1Engine::bottomLine(int startRow, int startCol)
{
    return QLine(startRow+1,startCol,startRow+1,startCol+1);
}

QLine Level1Engine::rightLine(int startRow, int startCol)
{
    return QLine(startRow,startCol+1,startRow+1,startCol+1);
}

QLine Level1Engine::leftLine(int startRow, int startCol)
{
    return QLine(startRow,startCol,startRow+1,startCol);
}


Q_EXPORT_PLUGIN2(level1engine, Level1Engine);

